from django.db import models
from django.core.exceptions import ValidationError

# 定义科室选项
DEPARTMENT_CHOICES = (
    ('综合眼科', '综合眼科'),
    ('小儿眼科', '小儿眼科'),
    ('眼底病科', '眼底病科'),
    ('角膜科', '角膜科'),
    ('青光眼专科', '青光眼专科'),
)
# 定义专业方向选项
SPECIALTY_CHOICES = (
    # 综合眼科专业方向
    ('眼表疾病', '眼表疾病'),
    ('泪道疾病', '泪道疾病'),
    ('眼屈光间质疾病', '眼屈光间质疾病'),
    # 小儿眼科专业方向
    ('儿童屈光不正', '儿童屈光不正'),
    ('儿童斜视与弱视', '儿童斜视与弱视'),
    ('儿童眼保健', '儿童眼保健'),
    # 眼底病科专业方向
    ('糖尿病视网膜病变', '糖尿病视网膜病变'),
    ('黄斑病变', '黄斑病变'),
    ('视网膜血管疾病', '视网膜血管疾病'),
    # 角膜科专业方向
    ('角膜移植', '角膜移植'),
    ('角膜屈光手术', '角膜屈光手术'),
    ('感染性角膜病', '感染性角膜病'),
    # 青光眼专科专业方向
    ('原发性青光眼', '原发性青光眼'),
    ('继发性青光眼', '继发性青光眼'),
    ('先天性青光眼', '先天性青光眼'),
)
# 定义职称选项
TITLE_CHOICES = (
    ('主治医师', '主治医师'),
    ('副主任医师', '副主任医师'),
    ('主任医师', '主任医师'),
    # 可以根据实际情况添加更多职称选项
)

# 医生基本信息模型
class Doctor(models.Model):
    name = models.CharField(max_length=64, verbose_name='医生姓名')
    title = models.CharField(max_length=32, verbose_name='职称', choices=TITLE_CHOICES)
    specialty = models.CharField(max_length=64, verbose_name='专业方向', choices=SPECIALTY_CHOICES)
    department = models.CharField(max_length=64, verbose_name='科室', choices=DEPARTMENT_CHOICES)
    secondary_specialty = models.CharField(max_length=64, blank=True, null=True, verbose_name='次专业方向')
    expertise = models.TextField(verbose_name='擅长项目')
    introduction = models.TextField(verbose_name='医生简介')
    sort = models.IntegerField(default=0, verbose_name='医生排序')
    real_register_count = models.IntegerField(default=0, verbose_name='真实挂号量')
    virtual_register_count = models.IntegerField(default=0, verbose_name='虚拟挂号量')
    avatar = models.ImageField(upload_to='doctor_avatars/%Y/%m/%d/', blank=True, null=True, verbose_name='医生头像')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')

    def total_register_count(self):
        return self.real_register_count + self.virtual_register_count

    class Meta:
        db_table = 'doctor'
        verbose_name = '医生'
        verbose_name_plural = verbose_name

# 医生排班模型
class DoctorSchedule(models.Model):
    doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE, related_name='schedules', verbose_name='医生')
    schedule_date = models.DateField(verbose_name='排班日期')
    start_time = models.TimeField(verbose_name='上班时间')
    end_time = models.TimeField(verbose_name='下班时间')
    is_on_duty = models.BooleanField(default=False, verbose_name='是否值班')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    def clean(self):
        if self.start_time and self.end_time and self.start_time >= self.end_time:
            raise ValidationError("上班时间必须早于下班时间")

    class Meta:
        db_table = 'doctor_schedule'
        verbose_name = '医生排班'
        verbose_name_plural = verbose_name

# 会诊记录模型
class ConsultRecord(models.Model):
    doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE, related_name='consult_records', verbose_name='会诊医生')
    patient_name = models.CharField(max_length=64, verbose_name='患者姓名')
    consult_time = models.DateTimeField(verbose_name='会诊时间')
    initiator = models.CharField(max_length=64, verbose_name='发起人')
    receiver = models.CharField(max_length=64, verbose_name='接受人')
    duration = models.CharField(max_length=16, verbose_name='会诊时长')
    status = models.CharField(max_length=32, verbose_name='问诊状态')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'consult_record'
        verbose_name = '会诊记录'
        verbose_name_plural = verbose_name